package win.ganbo.softwareFive.listener;

import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import win.ganbo.softwareFive.domain.LoginToken;
import win.ganbo.softwareFive.service.ILoginTokenService;

@WebListener
public class InitListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        WebApplicationContext beans = WebApplicationContextUtils
            .getWebApplicationContext(sce.getServletContext());
        ILoginTokenService loginTokenService = beans.getBean("loginTokenServiceImpl",
            ILoginTokenService.class);
        List<LoginToken> list = loginTokenService.getAll();
        if (list == null || list.size() == 0) { //11101020504
            long start = 11101020500L;
            long end = 11101020540L; //启动的时候产生40个登陆账号
            for (long i = start; i <= end; i++) {
                LoginToken loginToken = new LoginToken(i + "", i + "");
                loginTokenService.save(loginToken);
            }
        }

        System.out.println("初始化登陆账号信息完毕...");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }

}
